package apof.dao
{
	import flash.data.SQLConnection;
	import mx.messaging.config.ConfigMap;
	import apof.util.ConfigureReader;
	import flash.filesystem.File;
	import flash.data.SQLStatement;
	import flash.data.SQLResult;
	import flash.events.SQLEvent;
	
	public class DataSource
	{
		private var con:SQLConnection;
		public var conOpened:Boolean=false; 
		private var stmStack:Array=new Array();
		
		public function DataSource() {
			var filepath:String=ConfigureReader.cfg["DBFile"];
			var file:File=new File(filepath);
			con=new SQLConnection();
			con.addEventListener(SQLEvent.OPEN,conOpenHandler);
			con.open(file);
		}
		
		private function conOpenHandler(event:SQLEvent):void {
			conOpened=true;
			for each(var stm:SQLStatement in stmStack) {
				stm.execute();
			}
		}
		
		public function excute(stm:SQLStatement,callback:Function=null):void {
			stm.sqlConnection=con;
			var resultHandler:ResultHandler = new ResultHandler(stm,callback);
			stm.addEventListener(SQLEvent.RESULT,resultHandler.func);
			if(!conOpened)
				stmStack.push(stm)
			else stm.execute();
		}
	}
}